<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="工号" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入工号" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="标签" prop="tag">
        <el-input v-model="queryParams.tag" placeholder="请输入标签" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="总店" prop="zdId">
        <el-select v-model="queryParams.zdId" placeholder="请选择总店" clearable size="small">
          <el-option v-for="dict in zdIdOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否展示" prop="isShow">
        <el-select v-model="queryParams.isShow" placeholder="请选择是否展示" clearable size="small">
          <el-option v-for="dict in isShowOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="员工类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择员工类型" clearable size="small">
          <el-option v-for="dict in typeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['StarDesigner:StarDesignerZd:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['StarDesigner:StarDesignerZd:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['StarDesigner:StarDesignerZd:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['StarDesigner:StarDesignerZd:export']">导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" border stripe :data="StarDesignerZdList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="唯一ID" align="center" prop="id" /> -->
      <el-table-column label="工号" align="center" prop="code" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="标签" align="center" prop="tag" />
      <el-table-column label="员工类型" align="center" prop="type" :formatter="typeFormat" />
      <el-table-column label="员工简介" align="center" prop="info" />
      <el-table-column label="总店" align="center" prop="zdId" :formatter="zdIdFormat" />
      <el-table-column label="是否展示" align="center" prop="isShow" :formatter="isShowFormat" />
      <el-table-column label="备注" align="center" prop="remark" />
			<el-table-column label="头衔" align="center" prop="grade" />
			<el-table-column label="预约数" align="center" prop="caseNum" />
			<el-table-column label="服务客户数" align="center" prop="custNum" />
			<el-table-column label="从业时间" align="center" prop="serviceDate" />
			<el-table-column label="优秀案例数" align="center" prop="niceCaseNum" />
			<el-table-column label="浏览数" align="center" prop="readerNum" />
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermi="['StarDesigner:StarDesignerZd:edit']" plain type="primary" size="small" @click="handleUpdate(scope.row)">编辑</el-button>
          <br v-hasPermi="['StarDesigner:StarDesignerZd:remove']">
          <br v-hasPermi="['StarDesigner:StarDesignerZd:remove']">
          <el-button v-hasPermi="['StarDesigner:StarDesignerZd:remove']" plain type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['StarDesigner:StarDesignerZd:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['StarDesigner:StarDesignerZd:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改总店员工对话框 -->
    <el-dialog :title="title" :visible.sync="open" :before-close="cancel" :close-on-click-modal="false" :close-on-press-escape="false" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="工号" prop="code">
          <el-input v-model="form.code" placeholder="请点击选择1位员工" readonly="readonly" class="hover-style" v-on:click.native="openDeptUserSelector()" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请点击选择1位员工" readonly="readonly" class="hover-style" v-on:click.native="openDeptUserSelector()" />
        </el-form-item>
        <el-form-item label="标签" prop="tag">
          <el-input v-model="form.tag" placeholder="请输入标签且以英文逗号隔开,如'标签1,标签2,标签3...'" />
        </el-form-item>
        <el-form-item label="总店">
          <el-select v-model="form.zdId" placeholder="请选择总店">
            <el-option v-for="dict in zdIdOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否展示">
          <el-select v-model="form.isShow" placeholder="请选择是否展示">
            <el-option v-for="dict in isShowOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="员工类型">
          <el-select v-model="form.type" placeholder="请选择员工类型">
            <el-option v-for="dict in typeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="员工简介" prop="info">
          <el-input v-model="form.info" placeholder="请输入员工简介, 最多填写150个字" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注, 最多填写150个字" />
        </el-form-item>
				<el-form-item label="头衔" prop="grade">
				  <el-input v-model="form.grade" placeholder="请输入头衔" />
				</el-form-item>
				<el-form-item label="预约数" prop="caseNum">
				  <el-input v-model="form.caseNum" placeholder="请输入预约数" />
				</el-form-item>
				<el-form-item label="服务客户数" prop="custNum">
				  <el-input v-model="form.custNum" placeholder="请输入服务客户数" />
				</el-form-item>
				<el-form-item label="从业时间" prop="serviceDate">
				  <el-input v-model="form.serviceDate" placeholder="请输入从业时间(单位:年)" />
				</el-form-item>
				<el-form-item label="优秀案例数" prop="niceCaseNum">
				  <el-input v-model="form.niceCaseNum" placeholder="请输入优秀案例数" />
				</el-form-item>
				<el-form-item label="浏览数" prop="readerNum">
				  <el-input v-model="form.readerNum" placeholder="请输入浏览数" />
				</el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 选择人员信息 -->
    <el-dialog class="admin-selector" title="请选择1位员工" :visible.sync="deptUserSelectorOpen" :before-close="closeDeptUserSelector" :close-on-click-modal="false" :close-on-press-escape="false" width="800">
      <qywx-dept-user-selector ref="userSelector" :selectedList="selectedList" @onSelectUser="setSelectedList" @onDelUser="delSelectedList" :isSelectDept="false" :showTag="false" @onSelectDept="setSelectedList"></qywx-dept-user-selector>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="settingDeptUser">确 定</el-button>
        <el-button @click="closeDeptUserSelector">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listStarDesignerZd,
    getStarDesignerZd,
    delStarDesignerZd,
    addStarDesignerZd,
    updateStarDesignerZd,
    exportStarDesignerZd
  } from "@/api/StarDesigner/StarDesignerZd";
  import QywxDeptUserSelector from "@/components/QywxDeptUserSelector";

  export default {
    name: "StarDesignerZd",
    components: { QywxDeptUserSelector },
    data() {
      return {
        deptUserSelectorOpen: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 总店员工表格数据
        StarDesignerZdList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 总店ID字典
        zdIdOptions: [],
        // 是否展示字典
        isShowOptions: [],
        // 员工类型字典
        typeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          code: undefined,
          name: undefined,
          tag: undefined,
          zdId: undefined,
          isShow: undefined,
          type: undefined,
        },
        selectedList:[],
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
      this.getDicts("HN_STAR_DESIGN").then(response => {
        this.zdIdOptions = response.data;
      });
      this.getDicts("sys_yes_no").then(response => {
        this.isShowOptions = response.data;
        if (this.isShowOptions.length > 0) {
          this.isShowOptions.forEach(function(item) {
            if (item.dictValue == 'Y') {
              item.dictValue = 1;
            } else {
              item.dictValue = 0;
            }
          })
        }
      });
      this.getDicts("STAR_DESIGNER_ZD_TYPE").then(response => {
        this.typeOptions = response.data;
      });
    },
    methods: {
      settingDeptUser() {
        const that = this;
        if (that.selectedList.length > 0) {
          that.form.code = that.selectedList[0].userId;
          that.form.name = that.selectedList[0].userName;
          that.deptUserSelectorOpen = false;
        }
      },
      openDeptUserSelector(){
        const that = this;
        this.selectedList = []
        this.deptUserSelectorOpen = true
        setTimeout(function(){
          that.$refs.userSelector.getDeptTree()
        },40)
      },
      closeDeptUserSelector(){
        this.deptUserSelectorOpen = false
      },
      setSelectedList(item, val){
        let findIndex = this.selectedList.findIndex(function(obj){
          return (obj.userId && obj.userId == item.userId) || (!obj.userId && obj.id == item.id && obj.label == item.label)
        })
        if(val){
          if(findIndex==-1){
            this.selectedList.push(item)
          }
        }else{
          this.selectedList.splice(findIndex,1)
        }
      },
      delSelectedList(item){
        let findIndex = this.selectedList.findIndex(function(obj){
          return (obj.userId && obj.userId == item.userId) || (!obj.userId && obj.id == item.id && obj.label == item.label)
        })
        if(findIndex>-1){
          this.selectedList.splice(findIndex,1)
        }
      },
      /** 查询总店员工列表 */
      getList() {
        this.loading = true;
        listStarDesignerZd(this.queryParams).then(response => {
          this.StarDesignerZdList = response.rows;
          if (this.StarDesignerZdList.length > 0) {
            this.StarDesignerZdList.forEach(function(item) {
              if (item.isShow == 'Y' || item.isShow == 1) {
                item.isShow = 1;
              } else {
                item.isShow = 0;
              }
            })
          }
          this.total = response.total;
          this.loading = false;
        });
      },
      // 总店ID字典翻译
      zdIdFormat(row, column) {
        return this.selectDictLabel(this.zdIdOptions, row.zdId);
      },
      // 是否展示字典翻译
      isShowFormat(row, column) {
        return this.selectDictLabel(this.isShowOptions, row.isShow);
      },
      // 员工类型字典翻译
      typeFormat(row, column) {
        return this.selectDictLabel(this.typeOptions, row.type);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          createBy: undefined,
          createTime: undefined,
          updateTime: undefined,
          updateBy: undefined,
          isActive: undefined,
          lesseeId: undefined,
          code: undefined,
          name: undefined,
          tag: undefined,
          remark: undefined,
          zdId: undefined,
          isShow: undefined,
          type: undefined,
          info: undefined,
					grade: undefined,
					caseNum: undefined,
					custNum: undefined,
					serviceDate: undefined,
					niceCaseNum: undefined,
					readerNum: undefined,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.form.isShow = this.isShowOptions[0].dictValue;
        this.form.zdId = this.zdIdOptions[0].dictValue;
        this.title = "添加总店员工";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getStarDesignerZd(id).then(response => {
          this.form = response.data;
          if (this.form.isShow == 'Y' || this.form.isShow == 1) {
            this.form.isShow = 1;
          } else {
            this.form.isShow = 0;
          }
          this.open = true;
          this.title = "修改总店员工";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        if (this.loading) {
          return;
        }
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (!this.form.code || !this.form.name) {
              this.msgError("请点击选择1位员工");
              return;
            }
            if (!this.form.type) {
              this.msgError("请选择员工类型");
              return;
            }
            if (this.form.info && this.form.info.length > 150) {
              this.msgError("员工简介最多填写150个字");
              return;
            }
            if (this.form.remark && this.form.remark.length > 150) {
              this.msgError("备注最多填写150个字");
              return;
            }
            this.loading = true;
            if (this.form.id != undefined) {
              updateStarDesignerZd(this.form).then(response => {
                this.loading = false;
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addStarDesignerZd(this.form).then(response => {
                this.loading = false;
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除总店员工编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delStarDesignerZd(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有总店员工数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportStarDesignerZd(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      }
    }
  };
</script>
<style>
  .el-dialog__body {
    padding: 30px 30px 30px 0;
  }

  .el-dialog__header {
    width: 100%;
    text-align: center;
  }

  .el-dialog__body .el-select {
    width: 100%;
  }

  .el-dialog__footer {
    text-align: center;
  }

  .el-dialog__footer .el-button+.el-button {
    margin-left: 30px;
  }
  .hover-style input:hover{
    cursor: pointer;
  }
</style>
